
import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.io.File;


public class SubmitConfigDialog extends DialogWrapper {

    private String commonBaseUrl;
    private String packageName;
    private String enName;
    private String cnName;
    private String buildDetailFlag;
    private String detailCnName;
    private String detailEnName;

    protected SubmitConfigDialog(@Nullable Project project, String title) {
        super(project);
        init();
        // 对话框标题
        setTitle(title);
    }

    private JPanel center = new JPanel();
    private JPanel south = new JPanel();

    {
        // 对话框大小
        setSize(600, 600);
    }


    private final JLabel commonBaseUrlLabel = new JLabel(" 代码所在目录        (例如：D:\\code\\luffy.gaodunwangxiao.com\\)");
    private final DefaultValueTextField commonBaseUrlText = new DefaultValueTextField("D:\\code\\luffy.gaodunwangxiao.com\\");

    private final JFileChooser commonBaseUrlChooser = new JFileChooser();
    // 创建一个按钮触发目录选择操作
    private JButton selectCommonBasePathButton = new JButton("选择代码所在目录 (注：luffy项目 主pom.xml所在目录)");

    private final JLabel packageNameLabel = new JLabel(" 生成代码包名        (例如：book   注：小写)");
    private final DefaultValueTextField packageNameText = new DefaultValueTextField("fengyamin888");


    private final JLabel enNameLabel = new JLabel(" 主类英文名字        (例如：TeacherBook   注：和主类实体名保持一致)");
    private final DefaultValueTextField enNameText = new DefaultValueTextField("TeacherBook");

    private final JLabel cnNameLabel = new JLabel(" 主类中文名字        (例如：老师教材)");
    private final DefaultValueTextField cnNameText = new DefaultValueTextField("老师教材");

    private final JLabel buildDetailFlagLabel = new JLabel(" 是否生成子类        (否时无需填写下面子类两项配置)");
    private final JComboBox<Object> buildDetailFlagBox = new JComboBox<>();

    private final JLabel detailEnNameLabel = new JLabel(" 子类英文名字        (例如：TeacherBookDetail   注：和子类实体名保持一致)");
    private final DefaultValueTextField detailEnNameText = new DefaultValueTextField("TeacherBookDetail");

    private final JLabel detailCnNameLabel = new JLabel(" 子类中文名字        (例如：老师教材详情)");
    private final DefaultValueTextField detailCnNameText = new DefaultValueTextField("老师教材详情");


    // 创建一个方法来打开目录选择对话框并获取目录路径
    private void chooseCommonBasePath() {
        // 设置只能选择目录
        commonBaseUrlChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        // 'this' 指向当前窗口组件
        int result = commonBaseUrlChooser.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedDir = commonBaseUrlChooser.getSelectedFile();
            String dirPath = selectedDir.getAbsolutePath();
            // 更新 commonBaseUrl 变量和 commonBaseUrlText 的显示内容
            this.commonBaseUrl = dirPath;
            // 假设 commonBaseUrlText 是 JTextField，这里将其内容更新为选择的路径
            commonBaseUrlText.setText(dirPath);
        }
    }

    @Override
    protected @Nullable
    JComponent createCenterPanel() {
        // 面板布局
        GridLayout gridLayout = new GridLayout(18, 1);
        center.setLayout(gridLayout);
        center.add(commonBaseUrlLabel);
        center.add(commonBaseUrlText);
        selectCommonBasePathButton.addActionListener(e -> chooseCommonBasePath());
        center.add(selectCommonBasePathButton);
        center.add(new JLabel(" "));
        center.add(packageNameLabel);
        center.add(packageNameText);
        center.add(new JLabel(" "));
        center.add(enNameLabel);
        center.add(enNameText);
        center.add(cnNameLabel);
        center.add(cnNameText);
        center.add(new JLabel(" "));
        center.add(buildDetailFlagLabel);
        buildDetailFlagBox.addItem("是");
        buildDetailFlagBox.addItem("否");
        center.add(buildDetailFlagBox);
        center.add(detailEnNameLabel);
        center.add(detailEnNameText);
        center.add(detailCnNameLabel);
        center.add(detailCnNameText);
        return center;
    }

    @Override
    protected JComponent createSouthPanel() {
        // 定义Button，设置居中
        JButton button = new JButton("生成");
        button.setHorizontalAlignment(SwingConstants.CENTER);
        button.setVerticalAlignment(SwingConstants.CENTER);
        south.add(button);
        button.addActionListener(e -> {
            // 参数校验
            String info = validateInputs();
            if (!StrUtil.isEmpty(info)) {
                JOptionPane.showMessageDialog(null, info, "错误提示", JOptionPane.ERROR_MESSAGE);
                return;
            }

            // 校验通过，保存数据
            this.buildDetailFlag = buildDetailFlagBox.getSelectedItem().toString();
            this.commonBaseUrl = commonBaseUrlText.getText();
            this.packageName = packageNameText.getText();
            this.enName = enNameText.getText();
            this.cnName = cnNameText.getText();
            this.detailEnName = detailEnNameText.getText();
            this.detailCnName = detailCnNameText.getText();
            BuildCodeAction.buildFile(commonBaseUrl, cnName, enName, packageName, detailCnName, detailEnName, buildDetailFlag);
            //JOptionPane.showMessageDialog(null,commonBaseUrl,"测试",JOptionPane.ERROR_MESSAGE);
            dispose();
        });
        return south;
    }


    private String validateInputs() {
        // 1. 所有文本框内容都不为空
        if (commonBaseUrlText.getText().trim().isEmpty() ||
                packageNameText.getText().trim().isEmpty() ||
                enNameText.getText().trim().isEmpty() ||
                cnNameText.getText().trim().isEmpty()) {
            return "前四项不能为空";
        }

        // 2. 若选择生成子类，则子类名称不能为空
        if ("是".equals(buildDetailFlagBox.getSelectedItem().toString()) &&
                (detailEnNameText.getText().trim().isEmpty() || detailCnNameText.getText().trim().isEmpty())) {
            return "选择生成子类时后两项不可为空";
        }
        return null;
    }
}

